/*
 * PainelCidades.java
 *
 * Created on 29 de Outubro de 2006, 12:24
 */

package br.edu.ufcg.ccc.ai.genetictsp.gui;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;

import br.edu.ufcg.ccc.ai.genetictsp.utils.Point2DIF;

/**
 *
 * @author Gustavo de Farias
 * @author Diego Renato
 * @author Helder Fernando
 */
public class MapPanel extends javax.swing.JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Iterable<Point2DIF> cidades;
    private boolean executando;
    private ImageIcon iconCidade;
    private Point2DIF[] tour;
    
    private int descontoX;
    private int descontoY;
    
    /** Creates new form PainelCidades */
    public MapPanel() {
        initComponents();

        this.iconCidade = new ImageIcon(this.getClass().getResource("/br/edu/ufcg/ccc/ai/genetictsp/gui/images/city.png"));

        this.descontoX = this.iconCidade.getIconWidth() / 2;
        this.descontoY = this.iconCidade.getIconHeight() / 2;
        
        this.executando = false;
    }
    
    public void paint(Graphics g) {
        super.paint(g);
        
        if (executando) {
            
            Point2DIF p1 = this.tour[0];
            Point2DIF p2 = null;
            for (int i = 1; i < this.tour.length; i++) {
                
                p2 = this.tour[i];

                g.setColor(Color.red);
                g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
                
                p1 = p2;
            }
            
            p1 = this.tour[0];
            g.setColor(Color.red);
            g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
                        
            for (Point2DIF p : this.tour) {
                
                iconCidade.paintIcon(this, g, p.getX() - this.descontoX, p.getY() - this.descontoY);
            }
        }
        else {
            
            if (this.cidades != null) {
                
                for (Point2DIF p : this.cidades) {
                    
                    iconCidade.paintIcon(this, g, p.getX() - this.descontoX, p.getY() - this.descontoY);
                }
            }
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 255));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    void setListaCidades(Iterable<Point2DIF> listaCidades) {
        
        this.cidades = listaCidades;
    }

    void modoInicializacao() {
        
        this.executando = false;
    }

    void modoExecucao() {
        
        this.executando = true;
    }

    void setTour(Point2DIF[] tour) {
        
        this.tour = tour;
    }
    
}
